<?php 
class StringUtil {
  public static function shortText($text, $length) {
    $shortTitle = $text;
    if (strlen($text) > $length) {
      $shortTitle = trim(substr($text, 0, ($length - 3))) . "...";
    }
    return $shortTitle;
  }

  public static function generateRandomString($length = 55) {
    $characters = '1qazxsw23edcvfr45tgbnhy67ujmki89olp0';
    $string = "";

    for ($p = 0; $p < $length; $p++) {
      $string .= $characters[mt_rand(0, strlen($characters) - 1)];
    }

    return $string;
  }

  public static function hashPassword($password, $salt) {
    $string = md5(crypt(md5($password), md5($salt)));

    return trim($string);
  }

  public static function generateOfferCode($maxId) {
    return 'ST' . str_pad($maxId+1, 4, '0', STR_PAD_LEFT);
  }

  public static function getStatusArray() {
    return array(Constants::STATUS_ACTIVE => 'Active', Constants::STATUS_INACTIVE => 'Inactive');
  }

  public static function getGenderArray() {
    return array(Constants::GENDER_MALE => 'Male', Constants::GENDER_FEMALE => 'Female');
  }

  public static function getAccountTypeArray() {
    return array(Constants::USER_GROUP_MERCHANT_ADMIN => 'Admin', Constants::USER_GROUP_MERCHANT_USER => 'Limited');
  }

  public static function getTemplatesArray() {
    return array(Constants::TEMPLATE_IMAGE_LEFT => 'Image on Left', Constants::TEMPLATE_IMAGE_RIGHT => 'Image on Right', Constants::TEMPLATE_IMAGE_NONE => 'No Image', Constants::TEMPLATE_IMAGE_ONLY => 'Image Only');
  }

  public static function getDelimeter($route) {
    if (strpos($route, '?') !== false) {
      return '&';
    }
    return '?';
  }

  public static function getDisplayDate($date, $withTime = false) {
    $pattern = 'Y-m-d';
    if ($withTime) {
      $pattern .= ' H:i';
    }
    return date($pattern, strtotime($date));
  }

  public static function getTimeElapsed($referencedate = 0, $timepointer = '', $measureby = '', $autotext = true) {
    if ($timepointer == '') {
      $timepointer = time();
    }
    $raw = $timepointer - $referencedate;
    $clean = abs($raw);
    $calcNum = array(array('s', 60), array('m', 60*60), array('h', 60*60*60), array('d', 60*60*60*24), array('y', 60*60*60*24*365));
    $calc = array('s' => array(1, 'second'), 'm' => array(60, 'minute'), 'h' => array(60*60, 'hour'), 'd' => array(60*60*24, 'day'), 'y' => array(60*60*24*365, 'year'));

    if ($measureby == '') {
      $usemeasure = 's';

      for ($i=0; $i<count($calcNum); $i++) {
        if ($clean <= $calcNum[$i][1]) {
          $usemeasure = $calcNum[$i][0];
          $i = count($calcNum);
        }
      }
    } else {
      $usemeasure = $measureby;
    }

    $datedifference = floor($clean/$calc[$usemeasure][0]);

    if ($autotext == true && ($timepointer == time())) {
      if ($raw < 0) {
        $prospect = ' to go';
      } else {
        $prospect = ' to go';
      }
    }

    if ($referencedate != 0) {
      if ($datedifference == 1) {
        return $datedifference . ' ' . $calc[$usemeasure][1] . ' ' . $prospect;
      } else {
        return $datedifference . ' ' . $calc[$usemeasure][1] . 's ' . $prospect;
      }
    } else {
      return 'No input time referenced.';
    }
  }

  static public function slugify($text) {
    $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
    $text = trim($text, '-');
    if (function_exists('iconv')) {
      $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    }
    $text = strtolower($text);
    $text = preg_replace('~[^-\w]+~', '', $text);

    if (empty($text)) {
      return 'n-a';
    }

    return $text;
  }
}